//
//  UserAccount.swift
//  WeiBo(Swift)
//
//  Created by 周成龙 on 16/9/28.
//  Copyright © 2016年 ZCL. All rights reserved.
//

import UIKit

class UserAccount: NSObject, NSCoding {
    
    var access_token: String?
    var expires_in: Int = 0
    var uid: String?
    
    var screen_name: String?
    var avatar_large: String?
    
    // MARK: -生命周期
    init(dict: [String: Any]) {
        super.init()
        self.setValuesForKeys(dict)
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        
    }
    
    override var description: String {
        let keys = ["access_token", "expires_in", "uid"]
        let dict = dictionaryWithValues(forKeys: keys)
        return "\(dict)"
    }
    
    // MARK: -NSCoding
    func encode(with aCoder: NSCoder) {
        aCoder.encode(access_token, forKey: "access_token")
        aCoder.encode(expires_in, forKey: "expires_in")
        aCoder.encode(uid, forKey: "uid")
        aCoder.encode(screen_name, forKey: "screen_name")
        aCoder.encode(avatar_large, forKey: "avatar_large")
    }
    
    required init?(coder aDecoder: NSCoder) {
        self.access_token = aDecoder.decodeObject(forKey: "access_token") as? String
        self.expires_in = aDecoder.decodeInteger(forKey: "expires_in") as Int
        self.uid =  aDecoder.decodeObject(forKey: "uid") as? String
        self.screen_name = aDecoder.decodeObject(forKey: "screen_name") as? String
        self.avatar_large = aDecoder.decodeObject(forKey: "avatar_large") as? String
    }
    
    // MARK: -归档 解档
    
    static let filePath: String = "data.plist".cachesDirectory()
    
    func saveAccount() {
        NSKeyedArchiver.archiveRootObject(self, toFile: UserAccount.filePath)
    }
    
    static var userAccount: UserAccount?
    class func loadAccount() -> UserAccount? {
        
        if userAccount != nil {
            return userAccount
        }
        guard let account = NSKeyedUnarchiver.unarchiveObject(withFile: UserAccount.filePath) as? UserAccount else {
            return nil
        }
        userAccount = account
        return userAccount
    }
    
    //MARK: -外部控制
    class func isLogin() -> Bool {
        return UserAccount.loadAccount() != nil
    }
    
    // finished: (account: UserAccount?, error: Error?) -> ()
    //现在闭包作为参数传入的时候默认都是 @nonescaping 的, 如果闭包不是在当前作用域内执行而是还要继续传递到别的函数里的话, 那就必须加上 @escaping
    func loadUserInfo(finished: @escaping (_ account: UserAccount?, _ error: Error?) -> ()) {
        let path = "2/users/show.json"
        let dict = ["access_token": access_token!,"uid": uid!]
        NetworkTools.shareInstence.get(path, parameters: dict, progress: nil, success: { (task, objc) in
            ZLog(message: objc)
            
            guard let dic = objc as? [String: Any] else {
                return
            }
            self.avatar_large = dic["avatar_large"] as? String
            self.screen_name = dic["screen_name"] as? String
            finished(self, nil)
            
            }) { (task, error) in
                ZLog(message: error)
                finished(nil, error)
        }
    }
}
